package com.graduation_design.modules.app.controller;

import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.graduation_design.common.lang.Result;
import com.graduation_design.config.AlipayConfig;
import com.graduation_design.entity.AppOrder;
import com.graduation_design.modules.app.annotation.Login;
import com.graduation_design.modules.app.annotation.NoRepeatSubmit;
import com.graduation_design.service.AppOrderService;
import com.graduation_design.service.AppPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/app")
public class PayController {

    @Resource
    AppPayService appPayService;

    @Resource
    AlipayConfig alipayConfig;

    @Resource
    AppOrderService appOrderService;

    /**
     * 支付
     * @NoRepeatSubmit 避免重复支付
     */
    @Login
    @NoRepeatSubmit
    @PostMapping("/order/pay")
    public Result pay(String orderSn) {

        log.info("正在支付订单号 - {}", orderSn);
        return Result.success(appPayService.pay(orderSn));
    }

    /**
     * 主动查询
     */
    @Login
    @PostMapping("/pay/check")
    public Result payCheck(String orderSn) {
        if (StrUtil.isBlank(orderSn)) {
            return Result.fail("参数异常");
        }
        return Result.success(appPayService.payCheck(orderSn));
    }

    /**
     * 支付回调
     */
    @Login
    @PostMapping("/pay/notify")
    public void payNotify(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {

        PrintWriter out = response.getWriter();

        try {
            boolean flag = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipayPublicKey(), "GBK", "RSA2");

            if (flag) {
                // 商户订单号
                String outTradeNo = params.get("out_trade_no");

                // 支付宝交易号
                String tradeNo = params.get("trade_no");

                // 交易状态
                String tradeStatus = params.get("trade_status");

                if ("TRADE_SUCCESS".equals(tradeStatus)) {

                    AppOrder appOrder = appOrderService.getBySn(outTradeNo);
                    if (appOrder.getOrderStatus() == 0) {

                        appOrder.setOrderStatus(1);
                        appOrder.setPayTradeNo(tradeNo);
                        appOrder.setPayTime(LocalDateTime.now());
                        appOrderService.updateById(appOrder);
                        out.print("success");
                    }
                }
            }else {
                out.print("fail");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            out.print("fail");
        }

        out.flush();
        out.close();
        out = null;
    }
}
